Socket
Socket
Sign inDemoInstall

onml

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

onml

JSONML Library


Version published
Weekly downloads
1.8K
increased by41.27%
Maintainers
1
Weekly downloads
 
Created
Source

ONML

NPM version Actions Status

jsonml.org compatible tool set.

Use

Node.js

npm i onml --save
var onml = require('onml');

API

onml.parse() --- onml.p()

The onml.parse() method parses a XML/HTML/SVG string and returns a JavaScript value.

var obj = onml.parse('<text a="5">so me</text>');
console.log(obj);
-->
["text", {a: "5"}, "so me"]

onml.stringify() --- onml.s()

The onml.stringify(array, [indentation]) method converts a JavaScript value to a XML/HTML/SVG string.

var str = onml.stringify(['text', {a: 55}, 'so me'], 2);
console.log(str);
-->
<text a="55">
  so me
</text>

onml.traverse() --- onml.t()

JSONML object traversal tool. See test/traverse.js for more details.

onml.traverse(obj, {
    enter: function (node, parent) {
        ...
    },
    leave: function (node, parent) {
        ...
    }
});

Inside enter and leave functions:

node and parent objects have the following attributes:

  • .name -- tag name
  • .attr -- attributes object
  • .full -- full node array

this will hold additional methods:

  • this.name(string) -- to change the node tag
  • this.skip() -- to skip subtree based on the current node
  • this.remove() -- to remove current node
  • this.replace(array) -- to replace current node
// count divs on enter
var count = 0;
onml.traverse(
    ['b',
        ['div', {a: true},
            ['span',
                'div',
                ['div',
                    ['div', {},
                        ['div', {a: true}]
                    ]
                ],
                ['div', {},
                    ['div']
                ]
            ]
        ]
    ],
    {
        enter: function (node) {
            if (node.name === 'div') {
                count++;
            }
        }
    }
);
console.log(count);
-->
6

Testing

npm test

License

MIT LICENSE.

Keywords

FAQs

Package last updated on 19 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc